﻿#region "Copyright (C) Lenny Granovsky. 2011-2013"
//    This library provides extended cache capabilities to the ASP.NET applications.
//
//                Copyright (C) Lenny Granovsky. 2011-2013. 
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//    This program comes with ABSOLUTELY NO WARRANTY.
//    This is free software, and you are welcome to redistribute it
//    under certain conditions;
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Threading;

namespace FlexiCache
{
    /// <summary>
    /// Class facilitates the application and session variable storage for mixed (windows/web) applications; 
    /// </summary>
    [Serializable()]
    public sealed class RuntimeContext
    {
        private static RuntimeContextApplicationStorage appStorage;
        private static RuntimeContextSessionStorage sessionStorage;
        private static ICacheOnDemandProvider cacheOnDemandStorage;

        static RuntimeContext()
        {
            appStorage = new RuntimeContextApplicationStorage();
            sessionStorage = new RuntimeContextSessionStorage();
            cacheOnDemandStorage = new CacheOnDemandProvider();
            cacheOnDemandStorage.IsEnabled = CachingConfiguration.Current.CacheOnDemandConfig.IsEnabled;
        }

        private RuntimeContext()
        {
        }

        /// <summary>
        /// Returns true if HttpContext is currently available regardless if it comes from real ASP.NET runtime or Http Simulator.
        /// </summary>
        public static bool IsHttpContextAvailable
        {
            get
            {
                return (System.Web.HttpContext.Current != null);
            }
        }

        /// <summary>
        /// Returns true if runtime operates by NUnit Test environment.
        /// </summary>
        public static bool IsNUnitTestEnvironment
        {
            get
            {
                return (System.Threading.Thread.CurrentThread.Name == "TestRunnerThread");
            }
        }

        /// <summary>
        /// Gets application collection of key/object variables.
        /// </summary>
        public static RuntimeContextApplicationStorage ApplicationData
        {
            get
            {
                return appStorage;
            }
        }

        /// <summary>
        /// Gets session collection of key/object variables.
        /// </summary>
        public static RuntimeContextSessionStorage SessionData
        {
            get
            {
                return sessionStorage;
            }
        }

        /// <summary>
        /// Gets accessor to the Cache On Demand provider.
        /// </summary>
        public static ICacheOnDemandProvider CacheOnDemandData
        {
            get
            {
                return cacheOnDemandStorage;
            }
        }

        /// <summary>
        /// Gets accessor to the Session On Demand controller.
        /// </summary>
        public static ISessionOnDemandController SessionOnDemandData
        {
            get
            {
                return SessionOnDemandController.Current;
            }
        }
    }
}
